//
//  ResultTipView.m
//  Router
//
//  Created by teki on 15/10/15.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "ResultTipView.h"


#define SPACE_Y 16

@interface ResultTipView ()


@end

@implementation ResultTipView

-(id)init
{
    if (self = [super init])
    {
        [self initUI];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self initUI];
    }
    return self;
}

-(void)initUI
{
    self.backgroundColor=[UIColor whiteColor];
    
    self.imageView = [[UIImageView alloc] init];
    [self addSubview:_imageView];
    
    self.titleLabel= [self addLabelWithFontSize:15 textColor:SeparatorLineColor isBold:NO];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.numberOfLines= 0;
    
    
    [self makeConstraints];
}

-(void)readdActionButton
{
    if (_actionButton != nil) {
        [self.actionButton removeFromSuperview];
        self.actionButton = nil;
    }
  
    if (self.buttonClicked && self.buttonTitle) {
        self.actionButton = [self addWhiteButtonWithTitle:self.buttonTitle addTarget:self action:@selector(actionButtonClicked:)];
    }
}

-(void)makeConstraints
{
    
    [_imageView remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(80);
        make.centerX.equalTo(self);
    }];
    
    [_titleLabel remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_imageView.mas_bottom).offset(SPACE_Y);
        make.centerX.equalTo(self);
    }];
    
    
    [self readdActionButton];
    
    
    if (self.buttonTitle)
    {
        [_actionButton remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.bottom).offset(SPACE_Y);
            make.centerX.equalTo(self);
            make.size.equalTo(CGSizeMake(100, 40));
        }];
    }

    
}

-(void)setContentInfo
{
    self.imageView.image = [UIImage imageNamed:@"home_nonwifi"];
    self.titleLabel.text = self.title;
    [_actionButton setTitle:self.buttonTitle forState:UIControlStateNormal];
}



-(void)actionButtonClicked:(UIButton *)sender
{
    if (self.buttonClicked) {
        self.buttonClicked();
    }
    [self removeFromSuperview];
}



-(void)resetViewContent
{
    [self setContentInfo];
    [self makeConstraints];
}

-(void)dealloc
{
    self.buttonTitle = nil;
    self.titleLabel = nil;
    self.actionButton = nil;
}



#pragma mark -
#pragma mark   PrivateMethods
//白色圆角按钮
-(UIButton *)addWhiteButtonWithTitle:(NSString *)title addTarget:(id)target action:(SEL)action
{
    UIButton *btn=[self addButtonWithTitle:title fontSize:18 addTarget:target action:action];
    [btn setTitleColor:NavgationBarBackgroundColor forState:UIControlStateNormal];
    return btn;
}

//按钮
-(UIButton *)addButtonWithTitle:(NSString *)title fontSize:(CGFloat) fontSize addTarget:(id)target action:(SEL)action{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.translatesAutoresizingMaskIntoConstraints=NO;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    return btn;
}


//内容标签
-(UILabel *)addLabelWithFontSize:(int)fontSize textColor:(UIColor *)color isBold:(BOOL)bold
{
    UILabel *label=[[UILabel alloc]init];
    label.translatesAutoresizingMaskIntoConstraints=NO;
    label.backgroundColor=[UIColor clearColor];
    label.textColor=color;
    label.font=bold?[UIFont boldSystemFontOfSize:fontSize]:[UIFont systemFontOfSize:fontSize];
    [self addSubview:label];
    return label;
}
@end